class DinesController < ApplicationController
  before_filter :get_event,:only=>[:index]
  # GET /dines
  # GET /dines.xml
  def index
    @dines = @event.dines.paginate :page => params[:page], :per_page => 5

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @dines }
    end
  end

  def create
    @dine = Dine.new(params[:dine])
    @dine.event = Event.find(params[:event_id])
    @dine.save
  end


  
  def show_edit
    @dine = Dine.find(params[:id])
    render :partial=>'dines/edit_dine_form',:locals=>{:dine=>@dine}
  end

  def hide_edit
    render :text=>'';
  end
  # PUT /dines/1
  # PUT /dines/1.xml
  def update
    @dine = Dine.find(params[:id])
    if @dine.update_attributes(params[:dine])
      render :update do |page|
        page.replace "dine_#{@dine.id}", :partial=>"dines/dine_item",:locals=>{:dine=>@dine}
        page["dine_#{@dine.id}"].visual_effect :highlight
        page.replace_html("edit_place_#{@dine.id}", '')
      end
    end
  end

  # DELETE /dines/1
  # DELETE /dines/1.xml
  def delete
    @dine = Dine.find(params[:id])
    if @dine.destroy
      del(@dine)
    end
  end

  private
  def get_event
    @event = Event.find(params[:event_id])
  end

  def del(obj)
    if obj.destroy
      render :update do |page|
        page.remove("dine_#{obj.id}")
      end
    end
  end
end
